﻿Imports System.ServiceModel.DomainServices.Client
Imports LullabyFullDeCaixa.Web

Partial Public Class ConfigReportPlantillaHoraris
    Inherits ConfigReportAbstract

    Public Sub New()
        MyBase.New(My.Resources.StringLibrary.PlantillaDocuments, EnumTipusReport.PlantillaHoraris)
        InitializeComponent()

        Dim LListaDates As List(Of OpcioCombo) = (From o As OpcioCombo In OpcionsDatesHoraris.Llista Where DirectCast(o.Objecte, DateTime) > Now.AddMonths(-1)).ToList
        Me.cboData.ItemsSource = LListaDates

        If Contexte.IsHostalsLoaded Then
            OmpleDadesBuscador()
        Else
            Me.Activity.IsActive = True

            AddHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats

            Contexte.LoadHostals()
        End If

    End Sub

#Region "Generar"

    Private Sub GenerarReport(IdHostal As Integer, ByVal DataInici As OpcioCombo)
        Dim uri As String = BASE_URI & "?Report=" & Me.tipusReport

        If DataInici IsNot Nothing Then
            uri &= "&DataInici=" & DirectCast(DataInici.Objecte, Date).Ticks
        End If

        'afegim l'element filtrat
        uri &= "&IdHostal=" & IdHostal

        System.Windows.Browser.HtmlPage.Window.Eval("window.open('" & uri & "', '_blank')")
    End Sub

    Public Overrides Sub Generar()
        If Me.cboHostals.SelectedItem Is Nothing Then
            MessageBox.Show(StringLibrary.DebeSeleccionarUnHostal)
        ElseIf Me.cboData.SelectedItem Is Nothing Then
            MessageBox.Show(StringLibrary.DebeSeleccionarUnaFecha)
        Else
            GenerarReport(Me.cboHostals.SelectedItem.Id, Me.cboData.SelectedItem)
        End If
    End Sub

#End Region

#Region "Carregar Dades"
    Private Sub OmpleDadesBuscador()
        If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
            Me.cboHostals.ItemsSource = Contexte.Hostals.ToList
        Else
            Me.cboHostals.ItemsSource = Contexte.HostalsPerUsuariActual
        End If

        Me.cboHostals.SelectedItem = cboHostals.Items(0)
    End Sub

    Private Sub HostalsCarregats(Resultat As Boolean, Missatge As String)
        RemoveHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats

        If Contexte.IsHostalsLoaded Then
            Me.Activity.IsActive = False
            OmpleDadesBuscador()
        End If
    End Sub

#End Region


End Class
